// The form contains fields for many pagiantion optiosn so you can 
// quickly see the resuluts of the different options.
// This function creates an option object for the pagination function.
// This will be be unnecessary in your application where you just set
// the options once.
$(function() {
		var items_on_page = 10;
		var pages_step = 5;
		
		get_data(1, 10);
		$.ajax({
			type: "POST",
			url: "/stores/store_total",
			data: {
			name: $("#storeSearchSuggest").val(),
			category: $("#storeSearchCategory").val(),
			type: "get"},
			success: function(total)
			{
				$("#paging").jpaging({
					all_items_num: total,
					callback: get_data,
					items_on_page: items_on_page,
					pages_step: pages_step
				});
			}
		});

		$("#store_inner_search").click(function(){
                        get_data(1, 10);
                        $.ajax({
				type: "POST",
				url: "/stores/store_total",
				data: {
				name: $("#storeSearchSuggest").val(),
				category: $("#storeSearchCategory").val(),
				type: "get"},
                                success: function(total)
                                {
                                        $("#paging").jpaging({
                                                all_items_num: total,
                                                callback: get_data,
                                                items_on_page: items_on_page,
                                                pages_step: pages_step
                                        });
                                }
                        });
                        return false;
                });


		$("#storeSearchSuggest").focus().autocomplete("/stores/store_names", {
			width: 260,
			dataType:'json',
			selectFirst: false,
			minChars: 1,
			max:20,
			parse:function(data){
				var rows = new Array();
				for(var i = 0; i<data.length; i++){
					rows[rows.length] = {
					data:data[i],
					value:data[i],
					result:data[i]
					};
				}
				return rows;
			},
			formatItem:function(row,i,n){
				return row;
			}
			
		});

		$("#store_inner_search").click(function(){
			get_data(1, 10);
                        $.ajax({
                                type: "POST",
                                url: "/stores/store_total",
                                data: {name: $("#storeSearchSuggest").val(),
                                category: $("#storeSearchCategory").val(),
                                type: "get"},
                                success: function(total)
                                {
                                        $("#paging").jpaging({
                                                all_items_num: total,
                                                callback: get_data,
                                                items_on_page: items_on_page,
                                                pages_step: pages_step
                                        });
                                }
                        });
                        return false;
                });


		function get_data(start_index, end_index){
			start_index = start_index - 1;
			end_index = end_index - 1;

			$.ajax({
				type: "POST",
				url: "/stores/store_paging",            
				data: {start_index: start_index,
				end_index: end_index,
				name: $("#storeSearchSuggest").val(),
				category: $("#storeSearchCategory").val(),
				type: "get"},
				success: function(html)
				{                  
					$("#demo_tbl").html(html);
				}
			});			
		}

});
